﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace FySystem.Secure
{
    public class Des
    {
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DesEncrypt(string str)
        {
            string res = "";
            try
            {
                string key = "E526A308";                    //加密key
                string iv = "A888D6C1";                     //加密向量
                var des = DES.Create();
                byte[] arr = Encoding.UTF8.GetBytes(str);
                des.Key = Encoding.UTF8.GetBytes(key);
                des.IV = Encoding.UTF8.GetBytes(iv);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(arr, 0, arr.Length);
                cs.FlushFinalBlock();

                res = Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        /// <summary>
        /// Des加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string DesEncrypt(string str, string key, string iv)
        {
            string res = "";
            try
            {
                var des = DES.Create();
                byte[] arr = Encoding.UTF8.GetBytes(str);
                des.Key = Encoding.UTF8.GetBytes(key);
                des.IV = Encoding.UTF8.GetBytes(iv);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(arr, 0, arr.Length);
                cs.FlushFinalBlock();

                res = Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        /// <summary>
        /// Des解密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DesDecrypt(string str)
        {
            string res = "";
            try
            {
                string key = "E526A308";                    //加密key
                string iv = "A888D6C1";                     //加密向量
                var des = DES.Create();
                des.Key = Encoding.UTF8.GetBytes(key);
                des.IV = Encoding.UTF8.GetBytes(iv);
                byte[] arr = Convert.FromBase64String(str);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(arr, 0, arr.Length);
                cs.FlushFinalBlock();

                res = Encoding.UTF8.GetString(ms.ToArray());
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        /// <summary>
        /// Des解密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DesDecrypt(string str, string key, string iv)
        {
            string res = "";
            try
            {
                var des = DES.Create();
                des.Key = Encoding.UTF8.GetBytes(key);
                des.IV = Encoding.UTF8.GetBytes(iv);
                byte[] arr = Convert.FromBase64String(str);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(arr, 0, arr.Length);
                cs.FlushFinalBlock();

                res = Encoding.UTF8.GetString(ms.ToArray());
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }
    }
}
